<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" type="image/png" sizes="16x16" href="./assets/images/favicon.png">
    <title>图书管理系统</title>
    <link rel="stylesheet" type="text/css" href="./assets/extra-libs/multicheck/multicheck.css">
    <link href="./assets/libs/datatables.net-bs4/css/dataTables.bootstrap4.css" rel="stylesheet">
    <link href="./dist/css/style.min.css" rel="stylesheet">
    <script src="./assets/libs/jquery/dist/jquery.min.js"></script>
    <script src="./assets/libs/popper.js/dist/umd/popper.min.js"></script>
    <script src="./assets/libs/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="./assets/libs/perfect-scrollbar/dist/perfect-scrollbar.jquery.min.js"></script>
    <script src="./assets/extra-libs/sparkline/sparkline.js"></script>
    <script src="./dist/js/waves.js"></script>
    <script src="./dist/js/sidebarmenu.js"></script>
    <script src="./dist/js/custom.min.js"></script>
    <script src="./assets/extra-libs/multicheck/datatable-checkbox-init.js"></script>
    <script src="./assets/extra-libs/multicheck/jquery.multicheck.js"></script>
    <script src="./assets/extra-libs/DataTables/datatables.min.js"></script>
<!--    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>-->
<!--    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>-->
</head>
<body>
<div id="main-wrapper">
    <header class="topbar" data-navbarbg="skin5">
        <nav class="navbar top-navbar navbar-expand-md navbar-dark">
            <div class="navbar-header" data-logobg="skin5">
                <a class="nav-toggler waves-effect waves-light d-block d-md-none" href="javascript:void(0)"><i class="ti-menu ti-close"></i></a>
                <a class="navbar-brand" href="index.html">
                    <!-- Logo icon -->
                    <b class="logo-icon p-l-10">
                        <img src="./assets/images/logo-icon.png" alt="homepage" class="light-logo" />
                    </b>
                    <span class="logo-text">
                             <img src="./assets/images/logo-text.png" alt="homepage" class="light-logo" />
                        </span>
                </a>
                <a class="topbartoggler d-block d-md-none waves-effect waves-light" href="javascript:void(0)" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"><i class="ti-more"></i></a>
            </div>

            <div class="navbar-collapse collapse" id="navbarSupportedContent" data-navbarbg="skin5">

                <ul class="navbar-nav float-left mr-auto">
                    <li class="nav-item d-none d-md-block"><a class="nav-link sidebartoggler waves-effect waves-light" href="javascript:void(0)" data-sidebartype="mini-sidebar"><i class="mdi mdi-menu font-24"></i></a></li>

                </ul>

                <ul class="navbar-nav float-right">

                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle text-muted waves-effect waves-dark pro-pic" href="" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><img src="./assets/images/users/1.jpg" alt="user" class="rounded-circle" width="31"></a>
                        <div class="dropdown-menu dropdown-menu-right user-dd animated">
                            <div class="dropdown-divider"></div>
                            <a class="dropdown-item" href="index.html"><i class="fa fa-power-off m-r-5 m-l-5"></i> 注销</a>

                        </div>
                    </li>

                </ul>
            </div>
        </nav>
    </header>
    <aside class="left-sidebar" data-sidebarbg="skin5">
        <div class="scroll-sidebar">
            <nav class="sidebar-nav">
                <ul id="sidebarnav" class="p-t-30">
                    <li class="sidebar-item"><a class="sidebar-link waves-effect waves-dark sidebar-link" aria-expanded="false" href="manage.html"><i class="mdi mdi-view-dashboard"></i><span class="hide-menu">管理员主页</span></a></li>
                    <li class="sidebar-item"><a class="sidebar-link waves-effect waves-dark sidebar-link" aria-expanded="false" href="managequerybook.html"><i class="mdi mdi-view-dashboard"></i><span class="hide-menu">查询图书</span></a></li>
                    <li class="sidebar-item"><a class="sidebar-link waves-effect waves-dark sidebar-link" aria-expanded="false" href="usersearch.html"><i class="mdi mdi-view-dashboard"></i><span class="hide-menu">查询读者信息</span></a></li>
                    <li class="sidebar-item"><a class="sidebar-link waves-effect waves-dark sidebar-link" aria-expanded="false" href="addbook.html"><i class="mdi mdi-view-dashboard"></i><span class="hide-menu">添加图书</span></a></li>
                    <li class="sidebar-item"><a class="sidebar-link waves-effect waves-dark sidebar-link" aria-expanded="false" href="queryuserborrow.html"><i class="mdi mdi-view-dashboard"></i><span class="hide-menu">查询读者借阅</span></a></li>
                </ul>
            </nav>
        </div>
    </aside>

    <div class="page-wrapper">

        <div class="page-breadcrumb">
            <div class="row">
                <div class="col-12 d-flex no-block align-items-center">
                    <h4 class="page-title">您可以在这里发布公告或提醒已超期的读者及时还书。</h4>
                </div>
            </div>
        </div>

        <div class="container-fluid">

            <div class="row">
                <div class="col-12">

                    <div class="card">
                        <div class="card-body"><center>
                            <h5 class="card-title m-b-0">发布公告：</h5>
                            <div class="col-sm-9">
                                <textarea id="ntf"class="form-control" style="width: 60%"></textarea>
                            </div><br/>
                        <button type="button" class="btn btn-outline-dark" onclick="submit()">发布</button> </center><br/>
                            <div class="border-top"><br/>
                                <table id="notification" class="table table-striped table-bordered">
                                    <thead>
                                    <th>内容</th>
                                    <th>发布时间</th>
                                    <th>操作</th>
                                    </thead>
                                    <tbody id="ntflist">

                                    </tbody>
                                </table>
                            </div>

                        </div>

                    </div>

                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">以下用户有书超期未还，可进行提醒</h5>
                            <table id="shReturnUser" class="table table-striped table-bordered">
                                <thead>
                                <th>用户邮箱</th>
                                <th>未还书名</th>
                                <th>ISBN</th>
                                <th>应还日期</th>
                                <th>操作</th>
                                </thead>
                                <tbody id="userlist">

                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

        </div>
        <footer class="footer text-center"><br/><br/><br/><br/>Written by weh <a target="_blank"></a>
        </footer>
    </div>
</div>
<script type="text/javascript">
    $.ajax({
        type:"GET",
        url:"/notification",
        dataType:"json",
        success:function (result) {
            createnotifylist(result)
        }
    })

    $.ajax({
        type:"GET",
        url:"/borrow/borrowinfo/overdue",
        dataType:"json",
        success:function (result) {
            createuserlist(result)
        }
    })

    function createuserlist(data) {
        var str="";
        for(var i=0;i<data.length;i++){
            str+="<tr><td>"+data[i].userEmail+"</td><td>"+data[i].bookName+"</td><td>"+data[i].isbn+"</td><td>"+data[i].shReturnDate+"</td><td width='10%'><center>"+
                "<button type='button' class='btn btn-outline-dark' onclick=\"notice('"+data[i].userEmail+"','"+data[i].bookName+"')\">邮件提醒</button>"+
                "</center></td></tr>";
        }
        $("#userlist").html(str);
        $('#shReturnUser').DataTable();
    }

    function notice(email, bookName) {
        $.ajax({
            type:"POST",
            url:"/users/email",
            data:{
                email:email,
                bookName:bookName
            },
            dataType:"json",
            success:function (result) {
                if(result){
                    alert("发送成功")
                }
                else{
                    alert("发送失败")
                }
            }
        })
    }

    function createnotifylist(data) {
        var str="";
        for(var i=0;i<data.length;i++){
            str+="<tr><td width='75%'>"+data[i].content+"</td><td width='15%'>"+data[i].date+"</td><td width='10%'><center>"+
                "<button type='button' class='btn btn-outline-dark' onclick=\"deletentf("+data[i].id+")\">删除</button>"+
                "</center></td></tr>";
        }
        $("#ntflist").html(str);
        $('#notification').DataTable();
    }

    function submit() {
        var ntf=document.getElementById("ntf").value
        $.ajax({
            type:"POST",
            url:"/notification",
            data:{
                content:ntf
            },
            dataType:"json",
            success:function (result) {
                if (result) {
                    alert("添加公告成功")
                    location.reload()
                }
                else {
                    alert("添加失败")
                }
            }
        })
    }

    function deletentf(id) {

        var r=confirm("您确定要刪除该公告吗？")
        if(r){
            $.ajax({
                type:"DELETE",
                url:"/notification/"+id,
                dataType:"json",
                success:function (result) {
                    if(result){
                        alert("删除成功")
                        location.reload()
                    }
                    else{
                        alert("删除失败")
                    }
                }
            })
        }
    }

</script>
</body>
</html>